import {selectMyPmById, selectPmProgress} from '../api/myPerformance'
import {toScoreBySelf, toSetTarget, toTargetBaseInfo, toPmDetail} from '../libs/pageRedirect'
import useProgress from '../hooks/pm/progress'
import {getSession} from '@/libs/sessionStorage'
import {useRouter} from 'vue-router'

export const paramDispose = userId => {
  console.log('#待办跳转', location.href)
  // console.log('用户Id', userId)

  if (location.href.indexOf('?') != -1) {
    console.log('#待办跳转 携带了参数--------------------------------------')

    // 参数数据
    let pD = splitParam()
    // url前缀
    let uP = getUrlPrefix()
    console.log('#待办跳转 截取参数：', pD)
    console.log('#待办跳转 前缀url：', uP)

    // /myPerformance/home

    // 将参数存入本地
    localStorage.setItem('pmId', pD.pmId)

    setTimeout(() => {
      // location.href = uP
      window.history.replaceState({}, document.title, window.location.pathname)
      location.reload()
    }, 1000)
  } else {
    console.log('#待办跳转 未携带参数--------------------------------------')
    let localPmId = localStorage.getItem('pmId')
    // 先判断本地是否存在参数
    if (localPmId) {
      console.log(`#待办跳转 本地存在参数(#${localPmId}#)----------------------------`)
      localStorage.removeItem('pmId')
      console.log('#待办跳转 跳转页面')
      getCurrentPmProgressId(localPmId)
      // toPageF(localPmId)
      getPmDetailInfoByPmId(localPmId)
    } else {
      console.log(`#待办跳转 本地不存在参数----------------------------`)
      // 判断是否携带参数
    }
  }
}

let currentPmProgressId
let currentProgressStep

// 获取当前流程id
const getCurrentPmProgressId = async pmId => {
  await selectPmProgress({
    pmId: pmId
  }).then(res => {
    console.log('#待办跳转getCurrentPmProgressId', res)
    let allFlag = res.data.every(proc => proc.pmProgressCompleteStatus != 2)
    // 判断是否完成
    if (allFlag) {
      let currentPmProgress = res.data.find(item => {
        return item.pmProgressCompleteStatus == 1
      })
      console.log('#待办跳转getCurrentPmProgressId', currentPmProgress)
      currentPmProgressId = currentPmProgress.pmProgressId
      currentProgressStep = currentPmProgress.pmProgressStep
      setTimeout(() => {
        toPageF(pmId)
      }, 100)
    } else {
      currentProgressStep = 7
      setTimeout(() => {
        toPageF(pmId)
      }, 100)
    }
  })
}

// 跳转
const toPageF = async (pmId, procId = null) => {
  const router = useRouter()
  // const {getProgressById, progressById} = useProgress()
  console.log('#待办跳转 ' + currentPmProgressId)
  console.log('#待办跳转 ' + currentProgressStep)
  localStorage.removeItem('pmId')
  // let progress = await getProgressById({pmProgressId: pmId})
  switch (currentProgressStep) {
    case 1:
      console.log('#待办跳转 选择了跳转1')

      toSetTarget({
        pmId: pmId,
        progressId: currentPmProgressId
      })

      break
    case 2:
      console.log('#待办跳转 选择了跳转2')
      toTargetBaseInfo({
        defaultPmId: pmId,
        // title: itemd.workTaskContent.replace(/\S{2}:([^需要您的确认]{0,})需要您的确认$/, '$1'),
        // title: itemd.workTaskContent.replace(/\S{2}:([^需要您的确认]{0,})需要您的确认$/, '$1'),
        type: 'targetByLeader'
      })
      break
    case 4:
      console.log('#待办跳转 选择了跳转4')
      toScoreBySelf({
        defaultPmId: pmId
      })
      break
    case 6:
      console.log('#待办跳转 选择了跳转7')
      toTargetBaseInfo({
        defaultPmId: pmId,
        // title: itemd.workTaskContent.replace(/\S{2}:([^需要您的评分]{0,})需要您的评分$/, '$1'),
        type: 'scoreByLeader'
      })
    case 7:
      // let userDDId = getSession('userDDId')
      // ?individualDDId=16873072771893261
      // router.push({})
      toPmDetail(pmId)
      break
  }
}

// 通过pmId获取绩效详情信息
const getPmDetailInfoByPmId = async pmId => {
  await selectMyPmById({
    pmId
  }).then(res => {
    console.log('#待办跳转 获取绩效详情信息res', res)
  })
}

// 截取url前缀
const getUrlPrefix = () => {
  // let url = location.href
  let urlPrefix = ''
  // if (url.indexOf('?') != -1) {
  //   urlPrefix = url.split('?')[0]
  // } else {
  //   urlPrefix = url
  // }

  console.log(process.env.NODE_ENV)
  if (process.env.NODE_ENV == 'development') {
    urlPrefix = 'http://localhost:8083/#/myPerformance/home'
  } else {
    urlPrefix = 'http://d.lohand.com:8809/#/myPerformance/home'
  }

  return urlPrefix
}

// 截取参数
const splitParam = () => {
  let yPList = location.href.split('?')[1].split('&')
  let paramD = {}
  yPList.forEach(param => {
    let psd = param.split('=')
    let keyName = psd[0]
    let keyValue
    if (psd[1].indexOf('/') == -1 && psd[1].indexOf('#') == -1) {
      keyValue = psd[1]
    } else if (psd[1].indexOf('/') != -1) {
      keyValue = psd[1].split('/')[0].slice(0, -1)
    } else if (psd[1].indexOf('#') != -1) {
      keyValue = psd[1].split('#')[0].slice(0, -1)
    }
    paramD[keyName] = keyValue
  })
  return paramD
}
